package com.ruoyi.business.base.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.base.domain.AssetType;
import com.ruoyi.business.base.domain.vo.AssetTypeTreeSelect;
import com.ruoyi.business.base.mapper.AssetTypeMapper;
import com.ruoyi.business.base.service.IAssetTypeService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.uuid.SnowIdUtil;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 资产类别Service业务层处理
 *
 * @author lcy
 * @date 2024-09-24
 */
@Service
public class AssetTypeServiceImpl extends ServiceImpl<AssetTypeMapper, AssetType> implements IAssetTypeService {

    @Resource
    private AssetTypeMapper assetTypeMapper;

    /**
     * 查询资产类别
     *
     * @param id 资产类别主键
     * @return 资产类别
     */
    @Override
    public AssetType selectAssetTypeById(String id) {
        return assetTypeMapper.selectAssetTypeById(id);
    }

    /**
     * 查询资产类别列表
     *
     * @param assetType 资产类别
     * @return 资产类别
     */
    @Override
    public List<AssetType> selectAssetTypeList(AssetType assetType) {
        return assetTypeMapper.selectAssetTypeList(assetType);
    }

    /**
     * 新增资产类别
     *
     * @param assetType 资产类别
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor =  Exception.class)
    public Object insertAssetType(AssetType assetType) {
        assetType.setId(SnowIdUtil.genSnowId())
                    .setCreateBy(SecurityUtils.getUsername())
                    .setCreateTime(DateUtils.getNowDate())
                    .setUpdateBy(SecurityUtils.getUsername())
                    .setUpdateTime(DateUtils.getNowDate());
        assetTypeMapper.insert(assetType);
        return assetType.getId();
    }

    /**
     * 修改资产类别
     *
     * @param assetType 资产类别
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor =  Exception.class)
    public int updateAssetType(AssetType assetType) {
        assetType.setUpdateBy(SecurityUtils.getUsername())
                    .setUpdateTime(DateUtils.getNowDate());
        return assetTypeMapper.updateById(assetType);
    }

    /**
     * 批量删除资产类别
     *
     * @param ids 需要删除的资产类别主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor =  Exception.class)
    public int deleteAssetTypeByIds(String[] ids) {
        return assetTypeMapper.deleteAssetTypeByIds(ids);
    }

    /**
     * 查询资产类别树列表
     * @param assetType 资产类别
     * @return 树列表
     */
    @Override
    public List<AssetTypeTreeSelect> selectAssetTypeTreeList(AssetType assetType) {
        List<AssetType> assetTypes = SpringUtils.getAopProxy(this).selectAssetTypeList(assetType);
        return buildAssetTypeTreeSelect(assetTypes);
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param  assetTypes 资产类别列表
     * @return 下拉树结构列表
     */
    public List<AssetTypeTreeSelect> buildAssetTypeTreeSelect(List<AssetType> assetTypes)
    {
        List<AssetType> assetTypeList= buildAssetTypeTree(assetTypes);
        return assetTypeList.stream().map(AssetTypeTreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<AssetType> list, AssetType t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    /**
     * 得到子节点列表
     */
    private List<AssetType> getChildList(List<AssetType> list, AssetType t)
    {
        List<AssetType> tlist = new ArrayList<AssetType>();
        Iterator<AssetType> it = list.iterator();
        while (it.hasNext())
        {
            AssetType n = (AssetType) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals( t.getId()))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }


    /**
     * 递归列表
     */
    private void recursionFn(List<AssetType> list, AssetType t)
    {
        // 得到子节点列表
        List<AssetType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (AssetType tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }


    /**
     * 构建前端所需要树结构
     *
     * @param depts 部门列表
     * @return 树结构列表
     */
    public List<AssetType> buildAssetTypeTree(List<AssetType> assetTypes)
    {
        List<AssetType> returnList = new ArrayList<AssetType>();
        List<String> tempList = assetTypes.stream().map(AssetType::getId).collect(Collectors.toList());
        for (AssetType assetType : assetTypes)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(assetType.getParentId()))
            {
                recursionFn(assetTypes, assetType);
                returnList.add(assetType);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = assetTypes;
        }
        return returnList;
    }

}
